package com.sdtv.mediunfusion.mvp.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.sdtv.basemodule.bean.Results;
import com.sdtv.basemodule.mvp.presenter.BasePresenter;
import com.sdtv.basemodule.rx.BaseObserver;
import com.sdtv.mediunfusion.bean.PunchBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.model.PunchModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.mediunfusion.mvp.presenter
 * @ClassName: PunchPresenter
 * @Description: 打卡页面 的presenter
 * @Author: 小郭同学
 * @CreateDate: 2019-07-30 10:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PunchPresenter extends
        BasePresenter<MainContract.PunchView, PunchModel> {
    // 初始化model
    @Override
    public void initRepository() {
        mModel = new PunchModel(mView);
    }

    //获取打卡数据-可以是某一天 也可以是某一个月的数据
    public void getPunchLogging(String date) {
        mModel.getPunchLogging(date).subscribe(new BaseObserver<PunchBean>
                (PunchBean.class) {
            @Override
            public void doNextList(int totalCount ,List<PunchBean> response) {
                mView.setUserPunchInfo(response);
            }

            @Override
            public void onEmpty() {
                // 没有解析到数据 ---
                mView.setUserPunchInfo(new ArrayList<>());
            }

            @Override
            public void onError(Throwable e) {
                // 获取数据异常了 默认未打开
                mView.setUserPunchInfo(new ArrayList<>());
            }
        });
    }


    /**
     * 将打卡时间传递到后台
     *
     * @param location 打卡地址
     * @param status   是否在区域内打开  0：正常打卡 2：区域外打卡
     * @param time     打卡时间
     * @param remarks  备注内容 可为空
     */
    public void submitTime(String location, String status, String time, String remarks) {
        HashMap<String, String> map = new HashMap<>();
        map.put("time", time);
        //打卡坐标 --
        map.put("location", location);
        map.put("status", status);
        if (!TextUtils.isEmpty(remarks)) {
            map.put("remarks", remarks);
        }
        mModel.submitPunchMessage(map).subscribe(new BaseObserver() {
            @Override
            public void doResult(Results response) {
                LogUtils.e("result :" + response);
                mView.result(response);
            }

            @Override
            public void onError(Throwable e) {
                // 打开失败了--
                mView.punchFail();
            }

            @Override
            public void onEmpty() {
                // 没有返回信息 默认打开失败了--
                mView.punchFail();
            }
        });

    }


}
